package com.hisense.ovcloud.warehouse.dao;

import com.hisense.ovcloud.commons.tools.MurMurHashUtils;
import com.hisense.ovcloud.warehouse.Constants;
import com.hisense.ovcloud.warehouse.entity.julinkwarehouse.DeviceWarehouse;
import com.hisense.ovcloud.warehouse.entity.tableDef.DeviceWarehouseTableDef;
import com.hisense.ovcloud.warehouse.mapper.julinkwarehouse.DeviceWarehouseMapper;
import com.mybatisflex.annotation.UseDataSource;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
@UseDataSource(Constants.DS.SHARDING_JDBC)
public class DeviceWarehouseDao extends ServiceImpl<DeviceWarehouseMapper, DeviceWarehouse> {
    private final QueryWrapper queryWrapper;

    public DeviceWarehouseDao(QueryWrapper queryWrapper) {
        this.queryWrapper = queryWrapper;
    }


    /**
     * 查询全部
     */
    public List<DeviceWarehouse> selectAll(){
        return this.getMapper().selectAll();
    }


    public List<DeviceWarehouse> selectByWifiId(String wifiId){
        return this.getMapper().selectListByQuery(queryWrapper
                .select()
                .from(DeviceWarehouse.class)
                .where(DeviceWarehouseTableDef.DEVICE_WAREHOUSE.DEVICE_ID.eq(wifiId))
                .and(DeviceWarehouseTableDef.DEVICE_WAREHOUSE.SHARDING_KEY.eq(MurMurHashUtils.hash(wifiId)))
        );
    }

    public Page<DeviceWarehouse> selectBySubDomainPagable(String subDomainId, int pageNum, int pageSize){
        return this.getMapper().paginate(pageNum, pageSize, queryWrapper
                .select()
                .from(DeviceWarehouse.class)
                .where(DeviceWarehouseTableDef.DEVICE_WAREHOUSE.SUB_DOMAIN.eq(subDomainId))

        );
    }

    public long selectCountByBatchCode(String batchCode){
        return this.getMapper().selectCountByQuery(queryWrapper
                .select()
                .from(DeviceWarehouse.class)
                .where(DeviceWarehouseTableDef.DEVICE_WAREHOUSE.BATCH_CODE.eq(batchCode))
        );
    }

    public long selectCountBySubDomain(String subDomainId){
        return this.getMapper().selectCountByQuery(queryWrapper
                .select()
                .from(DeviceWarehouse.class)
                .where(DeviceWarehouseTableDef.DEVICE_WAREHOUSE.SUB_DOMAIN.eq(subDomainId))
        );
    }



    /**
     * Join
     */
//    public List<UserDto> getInfo(Integer userId){
//        QueryWrapper query = QueryWrapper.create()
//                .select(UserTableDef.USER.ALL_COLUMNS)
//                .select(SysDeptTableDef.SYS_DEPT.DEPT_NAME)
//                .from(UserTableDef.USER).as("u")
//                .leftJoin(SysDeptTableDef.SYS_DEPT).as("d").on(UserTableDef.USER.DEPT_CODE.eq(SysDeptTableDef.SYS_DEPT.DEPT_CODE))
//                .where(UserTableDef.USER.ID.eq(userId));
//        return this.getMapper().selectListByQueryAs(query,UserDto.class);
//    }

    /**
     * 新增
     */
    public Integer insert(DeviceWarehouse deviceWarehouse){
        deviceWarehouse.setSharding_key(MurMurHashUtils.hash(deviceWarehouse.getDevice_id()));
        return this.getMapper().insertSelective(deviceWarehouse);
    }

    /**
     * 更新
     */
    public void update(DeviceWarehouse deviceWarehouse){
        deviceWarehouse.setSharding_key(MurMurHashUtils.hash(deviceWarehouse.getDevice_id()));
        this.getMapper().update(deviceWarehouse);
    }

    /**
     * 局部更新
     */
    public void updateStatus(int status, String wifiId){
        UpdateChain.of(DeviceWarehouse.class)
                .set(DeviceWarehouse::getStatus, status)
                .where(DeviceWarehouseTableDef.DEVICE_WAREHOUSE.DEVICE_ID.eq(wifiId))
                .and(DeviceWarehouseTableDef.DEVICE_WAREHOUSE.SHARDING_KEY.eq(MurMurHashUtils.hash(wifiId)))
                .update();
    }

    /**
     * 删除
     */
    public void deleteByWrapper(String wifiId){
        this.getMapper().deleteByQuery(QueryWrapper.create().where(DeviceWarehouse::getDevice_id).eq(wifiId));
    }

}
